import 'package:flutter/material.dart';
import '../models/exhibition.dart';
import '../utils/theme_colors.dart';

class ExhibitionCard extends StatelessWidget {
  final Exhibition exhibition;
  final VoidCallback? onTap;
  final VoidCallback? onBookmark;
  final bool showHotBadge;
  final bool showCountdown;

  const ExhibitionCard({
    Key? key,
    required this.exhibition,
    this.onTap,
    this.onBookmark,
    this.showHotBadge = false,
    this.showCountdown = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.only(bottom: 16),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.08),
              blurRadius: 12,
              offset: const Offset(0, 4),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildImageSection(),
            _buildContentSection(),
          ],
        ),
      ),
    );
  }

  Widget _buildImageSection() {
    return Stack(
      children: [
        Container(
          height: 200,
          width: double.infinity,
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.vertical(top: Radius.circular(16)),
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                ThemeColors.primary.withOpacity(0.8),
                ThemeColors.secondary.withOpacity(0.8),
              ],
            ),
          ),
          child: const Center(
            child: Icon(
              Icons.museum,
              size: 48,
              color: Colors.white,
            ),
          ),
        ),
        // 状态标签
        Positioned(
          top: 12,
          left: 12,
          child: _buildStatusBadge(),
        ),
        // 热门标签
        if (showHotBadge)
          Positioned(
            top: 12,
            right: 12,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(12),
              ),
              child: const Text(
                '热门',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 10,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        // VR标签
        if (exhibition.hasVirtualTour)
          Positioned(
            bottom: 12,
            right: 12,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
              decoration: BoxDecoration(
                color: Colors.deepOrange,
                borderRadius: BorderRadius.circular(12),
              ),
              child: const Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(
                    Icons.view_in_ar,
                    size: 12,
                    color: Colors.white,
                  ),
                  SizedBox(width: 4),
                  Text(
                    'VR',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 10,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
          ),
        // 收藏按钮
        Positioned(
          bottom: 12,
          left: 12,
          child: GestureDetector(
            onTap: onBookmark,
            child: Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(0.9),
                shape: BoxShape.circle,
              ),
              child: Icon(
                exhibition.isBookmarked ? Icons.bookmark : Icons.bookmark_border,
                size: 20,
                color: exhibition.isBookmarked ? ThemeColors.primary : Colors.grey,
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildStatusBadge() {
    Color backgroundColor;
    String text;
    
    switch (exhibition.status) {
      case ExhibitionStatus.upcoming:
        backgroundColor = Colors.orange;
        text = '即将开展';
        break;
      case ExhibitionStatus.ongoing:
        backgroundColor = Colors.green;
        text = '正在展出';
        break;
      case ExhibitionStatus.completed:
        backgroundColor = Colors.grey;
        text = '已结束';
        break;
      case ExhibitionStatus.cancelled:
        backgroundColor = Colors.red;
        text = '已取消';
        break;
    }
    
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Text(
        text,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 10,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _buildContentSection() {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 标题和类型
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Text(
                  exhibition.title,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              const SizedBox(width: 8),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  color: ThemeColors.primary.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Text(
                  exhibition.type.displayName,
                  style: TextStyle(
                    fontSize: 10,
                    color: ThemeColors.primary,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          // 描述
          Text(
            exhibition.description,
            style: TextStyle(
              fontSize: 14,
              color: ThemeColors.textSecondary,
              height: 1.4,
            ),
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
          const SizedBox(height: 12),
          // 时间和地点
          Row(
            children: [
              Icon(
                Icons.access_time,
                size: 16,
                color: ThemeColors.textSecondary,
              ),
              const SizedBox(width: 4),
              Expanded(
                child: Text(
                  '${_formatDate(exhibition.startDate)} - ${_formatDate(exhibition.endDate)}',
                  style: TextStyle(
                    fontSize: 12,
                    color: ThemeColors.textSecondary,
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 4),
          Row(
            children: [
              Icon(
                Icons.location_on,
                size: 16,
                color: ThemeColors.textSecondary,
              ),
              const SizedBox(width: 4),
              Expanded(
                child: Text(
                  exhibition.location,
                  style: TextStyle(
                    fontSize: 12,
                    color: ThemeColors.textSecondary,
                  ),
                ),
              ),
            ],
          ),
          // 倒计时（如果需要显示）
          if (showCountdown && exhibition.status == ExhibitionStatus.upcoming)
            _buildCountdown(),
          const SizedBox(height: 12),
          // 底部信息
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              // 参观人数
              Row(
                children: [
                  Icon(
                    Icons.people,
                    size: 16,
                    color: ThemeColors.primary,
                  ),
                  const SizedBox(width: 4),
                  Text(
                    '${exhibition.visitCount}人参观',
                    style: TextStyle(
                      fontSize: 12,
                      color: ThemeColors.primary,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ],
              ),
              // 价格
              Text(
                exhibition.price > 0 ? '¥${exhibition.price}' : '免费',
                style: TextStyle(
                  fontSize: 14,
                  color: exhibition.price > 0 ? ThemeColors.primary : Colors.green,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildCountdown() {
    final now = DateTime.now();
    final difference = exhibition.startDate.difference(now);
    
    if (difference.isNegative) {
      return const SizedBox.shrink();
    }
    
    final days = difference.inDays;
    final hours = difference.inHours % 24;
    final minutes = difference.inMinutes % 60;
    
    return Container(
      margin: const EdgeInsets.only(top: 8),
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: Colors.orange.withOpacity(0.1),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            Icons.timer,
            size: 14,
            color: Colors.orange,
          ),
          const SizedBox(width: 4),
          Text(
            days > 0 ? '${days}天后开展' : '${hours}小时${minutes}分钟后开展',
            style: const TextStyle(
              fontSize: 11,
              color: Colors.orange,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  String _formatDate(DateTime date) {
    return '${date.month}/${date.day}';
  }
}